package android.mcsmtms.manager;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * Class GPSManager from http://code.google.com/p/krvarma-android-samples/ This
 * class handles the communication with the android device's gps receiver. It
 * accepts a callback <i>gpsCallBack</i> function to report the needed details
 * such as speed and coordinates.
 * */
public class GPSManager {
	private static final int gpsMinTime = 500;
	private static final int gpsMinDistance = 1;

	private static LocationManager locationManager = null;
	private static LocationListener locationListener = null;
	private static GPSCallback gpsCallback = null;
	
	private static final String TAG = GPSManager.class.toString();

	public GPSManager() {
		GPSManager.locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(final Location location) {
				if (gpsCallback != null) {
					gpsCallback.onGPSUpdate(location);
				}
			}

			@Override
			public void onProviderDisabled(final String provider) {
			}

			@Override
			public void onProviderEnabled(final String provider) {
			}

			@Override
			public void onStatusChanged(final String provider,
					final int status, final Bundle extras) {
			}
		};
	}

	public GPSCallback getGPSCallback() {
		return this.gpsCallback;
	}

	public void setGPSCallback(final GPSCallback gpsCallback) {
		this.gpsCallback = gpsCallback;
	}

	public void startListening(final Context context) {
		if (locationManager == null) {
			locationManager = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
		}

		 
		    // Define the criteria how to select the locatioin provider -> use
		    // default
	    Criteria criteria = new Criteria();
	    String provider = locationManager.getBestProvider(criteria, false);
//	    Location location = locationManager.requestLocationUpdates(provider, minTime, minDistance, listener);
		
		try {
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, gpsMinTime,
					gpsMinDistance, locationListener);
	    } catch (java.lang.SecurityException ex) {
	        Log.i(TAG, "fail to request location update, ignore", ex);
	    } catch (IllegalArgumentException ex) {
	        Log.d(TAG, "network provider does not exist " + ex.getMessage());
	    }
		
		try {
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, gpsMinTime,
					gpsMinDistance, locationListener);
	    } catch (java.lang.SecurityException ex) {
	        Log.i(TAG, "failed to request location update, ignore", ex);
	        Toast.makeText(context, "Failed to request location update!", Toast.LENGTH_SHORT).show();
	    } catch (IllegalArgumentException ex) {
	        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
	        Toast.makeText(context, "Gps provider does not exist!", Toast.LENGTH_SHORT).show();
	    }
	}

	public void stopListening() throws Exception {

		if (locationManager != null
				&& GPSManager.locationListener != null) {
			locationManager
					.removeUpdates(GPSManager.locationListener);
		}

		locationManager = null;
	}

	public boolean isProviderEnabled(String provider) {
		boolean isEnabled = locationManager.isProviderEnabled(provider);
		return isEnabled;
	}
}
